<?php

class AddTimes extends MY_Controller {

    public function __construct() {
        parent::__construct();

        if ($this->session->userdata('user_type') != 1 || $this->session->userdata('user_type') == null) {
            show_error("You have no permission here");
        }
    }

//    function searchCourse() {
//        $member = $_GET["user"];
//        $this->load->Model('Course_Permission_Model');
//        $listCourse = $this->Course_Permission_Model->getAllCourseByUsername($member);
//
//        $id = array();
//        $name = array();
//
//        if ($listCourse->num_rows() > 0) {
//            foreach ($listCourse->result_array() as $row) {
//                $id[] = $row['ID'];
//            }
//            foreach ($listCourse->result_array() as $row) {
//                $name[] = $row['NAME'];
//            }
//        }
//
//        $winetable = array(
//            "id" => $id,
//            "name" => $name,
//        );
//        print json_encode($winetable);
//
//    }

    function index() {

        $times = array(
            'name' => 'times',
            'id' => 'times'
        );
        $this->data['times'] = $times;

        $this->load->Model('Member_Model');
        $memberList = $this->Member_Model->getMemberList();
        $this->data['memberList'] = $memberList;

        $this->load->Model('Course_Model');
        $courseList = $this->Course_Model->getCourseList();
        $this->data['courseList'] = $courseList;

        $this->data['memberList'] = $memberList;
//        $memberList = $this->Member_Model->getMemberSelectList();
//        $this->data['memberList'] = $memberList;

        $this->data['main_content'] = 'add_times';
        $this->load->view('includes/template', $this->data);
    }

    function getVideoTimes($memberID) {

        $this->load->model('Member_Model');
        $member = $this->Member_Model->getMemberByID($memberID);

        echo json_encode($member->row()->TIMES);
//        header('Content-Type: application/x-json; charset=utf-8');
//        echo(json_encode($member->TIMES));
    }

    function Submit() {

        $username = $this->input->post('member');
        $course = $this->input->post('course');
        $times = $this->input->post('times');

        $this->load->model('Member_Model');
        $memberObj = $this->Member_Model->getMemberByUsername($username);

        $obj = $memberObj->row();

        $dataInsert = array(
            'MEMBER_ID' => $obj->ID,
            'COURSE_ID' => $course,
            'TIMES' => $times,
            'ACTIVE' => true);

        $dataUpdate = array(
            'TIMES' => $times,
            'ACTIVE' => true);

        $this->load->model('Course_Permission_Model');
        $result = $this->Course_Permission_Model->getCourseHaveOrNot($obj->ID, $course);
        if (!$result) {
            $this->db->insert('course_permission', $dataInsert);
        } else {
            $this->db->where("MEMBER_ID", $obj->ID);
            $this->db->where("COURSE_ID", $course);
            $this->db->update('course_permission', $dataUpdate);
        }
        $this->load->model('Course_Model');
        $courseObj = $this->Course_Model->getVideoTimesByCourseID($course);
        date_default_timezone_set('Asia/Bangkok');
        $dataLog = array(
            'COURSE_ID' => $courseObj->ID,
            'COURSE_NAME' => $courseObj->CODE,
            'MEMBER_ID' => $obj->ID,
            'MEMBER_NAME' => $obj->USERNAME,
            'REGIS_DATE' => date("Y-m-d H:i:s")
        );
        $this->db->insert('regis_course_log', $dataLog);

        redirect('Home');
    }

}

?>
